/*
 CS 491: IMDB
 Project 3
 */


//================Declare your variables here================
// Data structures for graph processing and displaying

TimeLine filmTimeLine;
GenreCheckBoxes genres;
Graph releaseGraph;
TopChart topMovies;
ClusterBar cluster;
DataAccess dataAccess;
PieChart pieChartTotal;
PieChart pieChartSelected;
Data [] dataArray;
String [] genreName = {
  "Action", "Adventure", "Animation", "Comedy", "Crime", "Documentary", 
  "Drama", "Fantasy", "Family", "Film-Noir", "Horror", "Musical", "Mystery", "Romance", "Sci-Fi", "Thriller", "War", "Western"
}; 
int [] count;

//Define the index of genres, so just use dataArray[Comedy] to get Comedy`s data.
final int Action = 0;
final int Adventure = 1;
final int Animation = 2;
final int Comedy = 3;
final int Crime = 4;
final int Documentary = 5;
final int Drama = 6;
final int Fantasy = 7;
final int Family = 8;
final int Film_Noir = 9;
final int Horror = 10;
final int Musical = 11;
final int Mystery = 12;
final int Romance = 13;
final int Sci_Fi = 14;
final int Thriller = 15;
final int War = 16;
final int Western = 17;
final int All = 18;
int loading;
ControlP5 control;
controlP5.Button help;

//================Declare variables end======================


void setup() {
  size(1280, 720);
  smooth();
  control = new ControlP5(this);
  //================Add variable setup here======================
  loading = 0;  
}

void draw() {
  
  background(0); 
  
  // initially show the message
  if(loading==0)
  {
      fill(255);
      textAlign(CENTER);
      textSize(28);
      text("Loading Data.\nPlease wait 30 sec...", width/2, height/2);      
  }
  
  if(loading==1)
  {
      fill(255);
      textAlign(CENTER);
      textSize(28);
      text("Loading Data.\nPlease wait...", width/2, height/2);
    
      filmTimeLine = new TimeLine(40, width-150, height-70, height-40,4);
      genres = new GenreCheckBoxes(control);
      releaseGraph = new Graph(40, width-270, 40, height - 80);
      
      topMovies = new TopChart(control, width - 260, width-10, 40, height - 80);
    
      cluster = new ClusterBar( width - 140, width - 10, height - 60, height - 50);
      int diameter = int((topMovies.y2-topMovies.y1)/4);
      pieChartTotal = new PieChart((int)topMovies.x1,(int)((topMovies.y2-topMovies.y1)/2),int(topMovies.x2-topMovies.x1),(int)((topMovies.y2-topMovies.y1)/2),diameter);
      pieChartSelected = new PieChart(5,(int)topMovies.x1,(int)((topMovies.y1+topMovies.y2)*3/4-50),int(topMovies.x2-topMovies.x1),(int)((topMovies.y2-topMovies.y1)/4),diameter);
      Clear(0); //clearing checked boxes
      dataAccess = new DataAccess(this); 
      dataArray = new Data [genreName.length];
      for( int i = 0  ;i<genreName.length;i++)
      {
        dataArray[i]= new Data();
      }
      count = new int[genreName.length];
      //println("genreName.length is "+genreName.length);
    
      dataAccess.getAllGenreData(dataArray);      
    
      for( int i =0;i< genreName.length;i++)
      {
        //old method,which should not be used any more
        //dataArray[i].copyData(dataAccess.getDataByGenre(genreName[i]));          
        count[i] = dataArray[i].getTotal();
        //println("Genre: "+genreName[i]+" initialized ! "+(genreName.length-(i+1))+" genres left...");
        println("The total number of "+genreName[i]+" is "+count[i]);
       
      }
      //dataArray[0].printRating();
       pieChartTotal.setupValue();
       help = control.addButton("helpButton",0,40,10,60,16);
  }
  
  //draw
  if(loading>1)

  {
    
    textAlign(LEFT); //align text for menu items
  
    smooth();
    rectMode(CORNER);
  
    filmTimeLine.draw();
    releaseGraph.draw();
    topMovies.draw();
    cluster.draw();
    genres.draw();
    pieChartTotal.draw();
    pieChartSelected.showLegend(false);
    pieChartSelected.draw();
  
    //align text for menu items
    textAlign(LEFT);
    }
    if(loading<2)
      increaseLoading();
      
}

//only increase when we have to so we are incremementing needlessly

public void increaseLoading()
{
  loading++;
}

public void Genres(int theValue) {
  genres.toggleGenres();
  topMovies.toggleLists();
}

public void Clear(int theValue) {
  genres.clear();
  releaseGraph.clear();
  topMovies.clear();
  pieChartSelected.clear();
}

// see if we're over any buttons, otherwise tell the map to drag
void mouseDragged() {
  boolean time = filmTimeLine.mouseOver();
  boolean clustering = cluster.mouseOver();
  if (time)
    filmTimeLine.mouseDragged();
  else if (clustering)
    cluster.mouseDragged();
}
void mousePressed()
{
  topMovies.mousePressed();
    
  //releaseGraph.mousePressed();
}
// see if we're over any buttons, and respond accordingly:

void mouseClicked() {
  //topMovies.mouseClicked();
 
  //ArrayList<Movie> m = new ArrayList<Movie>();
  //m = dataAccess.getTop5("Drama", m, 1920, 2010);
}
void keyReleased() {
  if(key == 'b' || key =='B'){
    pieChartTotal.changeColorMode();
    pieChartSelected.changeColorMode();
  }
}


void controlEvent(ControlEvent theEvent) {
  if(theEvent.isController()) {
    // an event from a controller e.g. button
    //println(theEvent.name() +":"+theEvent.controller().stringValue()+" from "+theEvent.controller());
  }
  else if (theEvent.isGroup()) {
    // an event from a group e.g. scrollList
    //println(theEvent.name() +":"+theEvent.group().value()+" from "+theEvent.group());
    if (theEvent.name() == "Movie Genres") {
      // a checkbox has been hit
      float[] checkedState = theEvent.group().arrayValue();
      //println(theEvent.group().arrayValue());
      int n = genres.getChosenCheckBox(checkedState);
      if (n < 0 || !genres.showGenre)
        return;
      if (checkedState[n] > 0) {
         //println("Adding (" + n + ") "+ releaseGraph.genreName[n] + " to graph");
        releaseGraph.addGraphType(n);
        topMovies.addTopList(n);
        pieChartSelected.addGenre(n);
      } else {
        //println("Removing (" + n + ") "+ releaseGraph.genreName[n] + " from graph");
        releaseGraph.removeGraphType(n);
        topMovies.removeTopList(n);
        pieChartSelected.removeGenre(n);
      } 
    }
  }
}

public void helpButton(int theValue) {
  //println("a button event from buttonA: "+theValue);
  link("https://sites.google.com/site/cs491group2/schedule");
}
public void update(int theValue) {
  println("a button event from updateButton: "+theValue);
  //link("http://code.google.com/p/cs491project2/");
}
public void yearList(int theValue) {
  println("a button event from yearList: "+theValue);
  //link("http://code.google.com/p/cs491project2/");
}

